// Fnio.h: interface for the CFnio class.
//
//////////////////////////////////////////////////////////////////////

#ifndef	_FNIO_HEADER_DEF_H
#define	_FNIO_HEADER_DEF_H
/*
#pragma message ( "Included CrevisFnIO.lib in file " __FILE__ )
#pragma comment (lib, "CrevisFnIO.lib")
*/
#include "CrevisFnIO.h"



typedef union _BYTE {
	unsigned char _byte;
	struct _bit {
		unsigned char B0 : 1;
		unsigned char B1 : 1;
		unsigned char B2 : 1;
		unsigned char B3 : 1;
		unsigned char B4 : 1;
		unsigned char B5 : 1;
		unsigned char B6 : 1;
		unsigned char B7 : 1;
	} Bbit;
} BINARY_BYTE;

typedef struct tagFNIODEVICE
{
	INT				DeviceID;
	INT				DeviceType;
	BYTE			IpAddress[4];
	INT				PortNum;
	UINT			InputSize;
	UINT			OutputSize;
	BINARY_BYTE*	pIo_Input;
	BINARY_BYTE*	pIo_Output;
} FNIODEVICE;


typedef struct tagDEVMODBUSTCP
{
	BYTE			IpAddress[4];
} DEVMODBUSTCP;


class CFnio  
{
public:
				CFnio();
	virtual		~CFnio();

public:
	// Basic functions
	INT			DevOpenDevice			(UINT* phDevice, DEVMODBUSTCP DevInfo);
	INT			DevCloseDevice			(UINT hDevice);
	INT			IoReadInputByteData		(UINT hDevice, WORD Address, BYTE* pBuffer, WORD Length);
	INT			IoReadOutputByteData	(UINT hDevice, WORD Address, BYTE* pBuffer, WORD Length);
	INT			IoWriteOutputByteData	(UINT hDevice, WORD Address, BYTE* pBuffer, WORD Length);
	INT			IoReadInputBitData		(UINT hDevice, WORD Address, BYTE BitIndex, BOOL* pBitData);
	INT			IoReadOutputBitData		(UINT hDevice, WORD Address, BYTE BitIndex, BOOL* pBitData);
	INT			IoWriteOutputBitData	(UINT hDevice, WORD Address, BYTE BitIndex, BOOL BitData);
	INT			DevReadDeviceStatus		(UINT hDevice, BYTE* pStatusdata);
	
	// Extention functions
	INT			DevGetIpAddress			(UINT hDevice, BYTE* pIpAddress);
	INT			DevGetPortNumber		(UINT hDevice, INT* pPortNum);
	INT			DevGetInputIoSize		(UINT hDevice, INT* pSize);
	INT			DevGetOutputIoSize		(UINT hDevice, INT* pSize);

protected:
	void		Init();
	void		Free();
	INT			GetAvailableDeviceIndex	(void);
	INT			GetDeviceIndex			(int DeviceID);
	void		ReleaseDevice			(int DevIdx);

protected:
	INT			m_Error;
	INT			m_OpnedDeviceCount;
	FNIODEVICE	m_Device[MAX_DEVICE];
	
};

#endif // _FNIO_HEADER_DEF_H
